home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / AppLauncher 1.0d0 / _(original postings) / UAEFinder.cp next >
Encoding:
Text File  |  1995-02-25  |  8.7 KB  |  365 lines  |  [TEXT/CWIE]

  1. //-------------------------------------------------------------------------------
  2. // UAEFinder.cp            Originally by Peter N Lewis (peter@mail.peter.com.au)
  3. //                        Converted to C++ by Gilles Dignard (gdignard@hookup.net)
  4. //-------------------------------------------------------------------------------
  5.  
  6. #include "UAEFinder.h"
  7.  
  8. Boolean
  9. UAEFinder::TellFinderToLaunch (FSSpec& ioSpec, Boolean inToFront)
  10. {
  11.     ProcessSerialNumber        process;
  12.     OSErr                    err;
  13.     OSErr                    junk;
  14.     AEDesc                    targetAddress;
  15.     AEDescList                fileList;
  16.     AppleEvent                theEvent;
  17.     AppleEvent                theReply;
  18.     AESendMode                sendmode;
  19.     long                    gv;
  20.     FSSpec                    finder_fs;
  21.     
  22.     err = -1;
  23.     if ((::Gestalt(gestaltFinderAttr, &gv) == noErr) 
  24.                     && BTST(gv, gestaltOSLCompliantFinder)) {
  25.         if (FindProcess ('MACS', 'FNDR', process, finder_fs)) {
  26.  
  27.             err = ::AECreateDesc(    typeProcessSerialNumber,
  28.                                     &process, sizeof(process),
  29.                                     &targetAddress);
  30.             
  31.             if (err == noErr)
  32.                 err = ::AECreateAppleEvent(    kCoreEventClass,
  33.                                             kAEOpenDocuments,
  34.                                             &targetAddress,
  35.                                             kAutoGenerateReturnID,
  36.                                             kAnyTransactionID,
  37.                                             &theEvent);
  38.             
  39.             if (err == noErr)
  40.                 err = ::AECreateList(nil, 0, false, &fileList);
  41.             
  42.             if (err == noErr)
  43.                 AddFSSToAEList(fileList, 1, ioSpec);
  44.             
  45.             if (err == noErr)
  46.                 err = ::AEPutParamDesc(&theEvent, keyDirectObject, &fileList);
  47.             
  48.             if (err == noErr) {
  49.                 sendmode = kAENoReply;
  50.                 if (!inToFront)
  51.                     sendmode = sendmode + kAENeverInteract;
  52.                 
  53.                 err = ::AESend(    &theEvent,
  54.                                 &theReply,
  55.                                 sendmode,
  56.                                 kAEHighPriority,
  57.                                 kNoTimeOut,
  58.                                 nil, nil);
  59.             }
  60.             
  61.             if ((err == noErr) && (inToFront))
  62.                 junk = ::SetFrontProcess(&process);
  63.  
  64.             ::AEDisposeDesc(&theEvent);
  65.             ::AEDisposeDesc(&theReply);
  66.             ::AEDisposeDesc(&fileList);
  67.             ::AEDisposeDesc(&targetAddress);            
  68.  
  69.         }    // FindProcess
  70.     }    // Gestalt
  71.     return (err == noErr);
  72. }
  73.  
  74. Boolean
  75. UAEFinder::OpenControlPanel (OSType inCreator)
  76. {
  77.     Boolean theResult = false;
  78.     
  79.     FSSpec theSpec;
  80.     
  81.     if (FindControlPanel(inCreator, theSpec) == noErr)
  82.         theResult = TellFinderToLaunch(theSpec, true);
  83.     
  84.     return theResult;
  85. }
  86.  
  87.  
  88. void
  89. UAEFinder::LaunchWithDocument (OSType inCreator, OSType inType, FSSpec& inSpec, Boolean inToFront)
  90. {
  91.     ProcessSerialNumber psn;
  92.     AEDesc targetAddress;
  93.     AppleEvent theEvent;
  94.     AppleEvent theReply;
  95.     AEDescList fileList;
  96.     FSSpec app_fs;
  97.     OSErr oe;
  98.     long gv;
  99.     AESendMode sendmode;
  100.     long t;
  101.     long c;
  102.     
  103.     ::PurgeSpace(&t, &c);
  104.     if ((::Gestalt(gestaltOSAttr, &gv) == noErr)
  105.      && (BTST(gv, gestaltLaunchControl))
  106.      && (c > 4096)) {
  107.         if (FindProcess(inCreator, inType, psn, app_fs)) {
  108.             oe = ::AECreateDesc(typeProcessSerialNumber, &psn, sizeof(psn), &targetAddress);
  109.             oe = ::AECreateAppleEvent(    kCoreEventClass,
  110.                                         kAEOpenDocuments,
  111.                                         &targetAddress,
  112.                                         kAutoGenerateReturnID,
  113.                                         kAnyTransactionID,
  114.                                         &theEvent);
  115.             oe = ::AEDisposeDesc(&targetAddress);
  116.             oe = ::AECreateList(nil, 0, false, &fileList);
  117.             AddFSSToAEList(fileList, 1, inSpec);
  118.             oe = ::AEPutParamDesc(&theEvent, keyDirectObject, &fileList);
  119.             sendmode = kAENoReply;
  120.             if (!inToFront)
  121.                 sendmode += kAENeverInteract;
  122.             oe = ::AESend(    &theEvent,
  123.                             &theReply,
  124.                             sendmode,
  125.                             kAEHighPriority,
  126.                             kNoTimeOut,
  127.                             nil, nil);
  128.             oe = ::AEDisposeDesc(&theEvent);
  129.             oe = ::AEDisposeDesc(&theReply);
  130.             oe = ::AEDisposeDesc(&fileList);
  131.             if (!inToFront)
  132.                 oe = ::SetFrontProcess(&psn);
  133.         }
  134.     }
  135. }
  136.  
  137.  
  138. void
  139. UAEFinder::LaunchApp (OSType inCreator, OSType inType, Boolean inToFront)
  140. {
  141.     ProcessSerialNumber psn;
  142.     FSSpec app_fs;
  143.     OSErr oe;
  144.     long gv;
  145.     
  146.     if ((::Gestalt(gestaltOSAttr, &gv) == noErr) && (BTST(gv, gestaltLaunchControl))) {
  147.         if (FindProcess(inCreator, inType, psn, app_fs)) {
  148.             if (inToFront)
  149.                 oe = ::SetFrontProcess(&psn);
  150.         } else {
  151.             if (FindApplication(inCreator, app_fs) == noErr)
  152.                 LaunchFSSpec(app_fs, inToFront);
  153.         }
  154.     }
  155. }
  156.  
  157.  
  158. void
  159. UAEFinder::LaunchFSSpec (FSSpec& ioSpec, Boolean inToFront)
  160. {
  161.     OSErr oe;
  162.     FInfo fi;
  163.     AEDesc targetAddress;
  164.     AppleEvent theEvent;
  165.     long gv;
  166.     LaunchParamBlockRec launchThis;
  167.     AEDesc launchDesc;
  168.     
  169.     if ((::Gestalt(gestaltOSAttr, &gv) == noErr) && (BTST(gv, gestaltLaunchControl))) {
  170.         oe = ::FSpGetFInfo(&ioSpec, &fi);
  171.         oe = ::AECreateDesc(    typeApplSignature,
  172.                                 &fi.fdCreator,
  173.                                 sizeof(fi.fdCreator),
  174.                                 &targetAddress);
  175.         oe = ::AECreateAppleEvent(    kCoreEventClass,
  176.                                     kAEOpenApplication,
  177.                                     &targetAddress,
  178.                                     kAutoGenerateReturnID,
  179.                                     kAnyTransactionID,
  180.                                     &theEvent);
  181.         oe = ::AEDisposeDesc(&targetAddress);
  182.         launchThis.launchAppSpec = &ioSpec;
  183.         PrepareToLaunch(theEvent, inToFront, launchDesc, launchThis);
  184.         oe = ::LaunchApplication(&launchThis);
  185.         oe = ::AEDisposeDesc(&theEvent);
  186.     }
  187. }
  188.  
  189. void
  190. UAEFinder::QuitApplication (OSType inCreator, OSType inType)
  191. {
  192.     ProcessSerialNumber process;
  193.     AEAddressDesc targetAddress;
  194.     AppleEvent theEvent;
  195.     AppleEvent theReply;
  196.     FSSpec fs;
  197.     OSErr oe;
  198.     
  199.     if (FindProcess(inCreator, inType, process, fs)) {
  200.         oe = ::AECreateDesc(    typeProcessSerialNumber,
  201.                                 &process,
  202.                                 sizeof(process),
  203.                                 &targetAddress);
  204.         oe = ::AECreateAppleEvent(    kCoreEventClass,
  205.                                     kAEQuitApplication,
  206.                                     &targetAddress,
  207.                                     kAutoGenerateReturnID,
  208.                                     kAnyTransactionID,
  209.                                     &theEvent);
  210.         oe = ::AEDisposeDesc(&targetAddress);
  211.         oe = ::AESend(    &theEvent,
  212.                         &theReply,
  213.                         kAENoReply,
  214.                         kAEHighPriority,
  215.                         5 * 60,
  216.                         nil, nil);
  217.         oe = ::AEDisposeDesc(&theEvent);
  218.         oe = ::AEDisposeDesc(&theReply);
  219.     }
  220. }
  221.  
  222.  
  223. OSErr
  224. UAEFinder::FindApplication (OSType inCreator, FSSpec& ioSpec)
  225. {
  226.     short i;
  227.     DTPBRec pbdt;
  228.     long freebytes;
  229.     OSErr oe;
  230.     Boolean found;
  231.     long gv;
  232.     
  233.     found = false;
  234.     if (::Gestalt(gestaltSystemVersion, &gv) && (gv >= 0x0700)) {    // has system 7
  235.         i = 1;
  236.         do {
  237.             ioSpec.vRefNum = 0;
  238.             oe = ::GetVInfo(i, ioSpec.name, &ioSpec.vRefNum, &freebytes);
  239.             i++;
  240.             if (oe == noErr) {
  241.                 ioSpec.name[0] = 0;
  242.                 pbdt.ioNamePtr = ioSpec.name;
  243.                 oe = ::PBDTGetPath(&pbdt);
  244.                 if (oe == noErr) {
  245.                     pbdt.ioIndex = 0;
  246.                     pbdt.ioFileCreator = inCreator;
  247.                     oe = ::PBDTGetAPPLSync(&pbdt);
  248.                     if (oe == noErr)
  249.                         found = true;
  250.                 }
  251.                 oe = noErr;
  252.             }
  253.         } while ((!found) && (oe == noErr));
  254.     }
  255.     if (found) {
  256.         oe = noErr;
  257.         ioSpec.parID = pbdt.ioAPPLParID;
  258.     } else {
  259.         oe = afpItemNotFound;
  260.         ioSpec.vRefNum = 0;
  261.         ioSpec.parID = 2;
  262.         ioSpec.name[0] = 0;
  263.     }
  264.     return oe;
  265. }
  266.  
  267.  
  268. Boolean
  269. UAEFinder::FindProcess (OSType inCreator, OSType inType,
  270.              ProcessSerialNumber& ioProcess, FSSpec& ioSpec)
  271. {
  272.     ProcessInfoRec info;
  273.     long gv;
  274.     
  275.     Boolean processWasFound = false;
  276.     
  277.     if ((::Gestalt(gestaltOSAttr, &gv) == noErr) && (BTST(gv, gestaltLaunchControl))) {
  278.         ioProcess.highLongOfPSN = 0;
  279.         ioProcess.lowLongOfPSN = kNoProcess;
  280.         info.processInfoLength = sizeof(ProcessInfoRec);
  281.         info.processName = nil;
  282.         info.processAppSpec = &ioSpec;
  283.         while (::GetNextProcess(&ioProcess) == noErr) {
  284.             if (::GetProcessInformation(&ioProcess, &info) == noErr) {
  285.                 if ((info.processType == (long) inType)
  286.                  && (info.processSignature == inCreator)) {
  287.                     processWasFound = true;
  288.                     break;
  289.                 }
  290.             }
  291.         }
  292.     }
  293.     
  294.     return processWasFound;
  295. }
  296.  
  297.  
  298. OSErr
  299. UAEFinder::FindControlPanel (OSType inCreator, FSSpec& ioSpec)
  300. {
  301.     HParamBlockRec    pb;
  302.     short            i;
  303.     OSErr            theErr;
  304.     
  305.     theErr = ::FindFolder(kOnSystemDisk, kControlPanelFolderType, false,
  306.                             &ioSpec.vRefNum, &ioSpec.parID);
  307.     if (theErr == noErr) {
  308.         i = 1;
  309.         do {
  310.             pb.fileParam.ioNamePtr = ioSpec.name;
  311.             pb.fileParam.ioVRefNum = ioSpec.vRefNum;
  312.             pb.fileParam.ioFRefNum = ioSpec.parID;
  313.             pb.fileParam.ioFDirIndex = i;
  314.             i++;
  315.             theErr = ::PBHGetFInfoSync(&pb);
  316.             if (theErr == noErr) {
  317.                 if (   (pb.fileParam.ioFlFndrInfo.fdType == 'cdev')
  318.                     && (pb.fileParam.ioFlFndrInfo.fdCreator == inCreator))
  319.                     break;
  320.             }
  321.         } while (theErr == noErr);
  322.     }
  323.     return theErr;
  324. }
  325.  
  326.  
  327. Boolean
  328. UAEFinder::BTST (long inLong, short inBitNo)
  329. {
  330.     return ((inLong >> inBitNo) & 1);    // ####
  331. }
  332.  
  333. void
  334. UAEFinder::PrepareToLaunch (AppleEvent &ioEvent, Boolean toFront,
  335.                  AEDesc &ioLaunchDesc, LaunchParamBlockRec &ioLaunchThis)
  336. {
  337.     OSErr oe;
  338.     
  339.     oe = ::AECoerceDesc(&ioEvent, typeAppParameters, &ioLaunchDesc);
  340.     HLock((Handle) ioEvent.dataHandle);
  341.     ioLaunchThis.launchAppParameters = (AppParametersPtr) *(ioLaunchDesc.dataHandle);
  342.     ioLaunchThis.launchBlockID = extendedBlock;
  343.     ioLaunchThis.launchEPBLength = extendedBlockLen;
  344.     ioLaunchThis.launchFileFlags = 0;
  345.     ioLaunchThis.launchControlFlags = launchContinue + launchNoFileFlags;
  346.     if (!toFront)
  347.         ioLaunchThis.launchControlFlags += launchDontSwitch;
  348. }
  349.  
  350.  
  351. void
  352. UAEFinder::AddFSSToAEList (AEDescList& ioList, short inRow, FSSpec& ioSpec)
  353. {
  354.     AliasHandle    fileAlias;
  355.     OSErr        theErr;
  356.     
  357.     theErr = ::NewAlias(nil, &ioSpec, &fileAlias);
  358.     if (theErr == noErr) {
  359.         ::HLock((Handle) fileAlias);
  360.         theErr = ::AEPutPtr(&ioList, inRow, typeAlias,
  361.                             (Ptr) *fileAlias, (*fileAlias)->aliasSize);
  362.         ::DisposeHandle((Handle) fileAlias);
  363.     }
  364. }
  365.